/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.element;

import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.enums.ElementType;
import pa165.dominating.species.tos.ElementTo;


import pa165.dominatingspecies.frontend.pages.BasePage;
import pa165.dominatingspecies.frontend.security.Authorizer;
import pa165.dominatingspecies.service.impl.ElementServiceImpl;

/**
 *
 * @author Ondrej Prazak
 */
@PreAuthorize("hasRole('WORKER')")
public class ElementsPage extends BasePage {

    @SpringBean(name = "elementService")
    private ElementServiceImpl elementService;

    public ElementsPage() {
        initGui();
    }

    private void initGui() {
        addElementsModule();

        addNewElementLink();

    }

    private void addElementsModule() {
        ListView<ElementTo> listView = new ListView<ElementTo>("listview", createModelForElements()) {
            @Override
            protected void populateItem(ListItem<ElementTo> item) {
                item.add(new Label("id", new PropertyModel(item.getModel(), "id")));
                final ElementTo ele = (ElementTo) item.getModelObject();
                item.add(new Label("type", new EnumPropertyModel<ElementType>(ele, "type", this)));

                Link<BasePage> edit = new Link<BasePage>("edit") {
                    @Override
                    public void onClick() {
                        EditElementPage ep = new EditElementPage(ele.getId());
                        setResponsePage(ep);
                    }
                };
                item.add(edit);

                Link deleteElementLink = new DeleteElementLink("delete", item.getModelObject());
                Authorizer.authorize(deleteElementLink);
                item.add(deleteElementLink);
                

                Link<BasePage> details = new Link<BasePage>("details") {
                    @Override
                    public void onClick() {
                        ElementDetailsPage edp = new ElementDetailsPage(ele.getId());
                        setResponsePage(edp);
                    }
                };
                item.add(details);

            }
        };
        add(listView);
    }

    private LoadableDetachableModel<List<ElementTo>> createModelForElements() {

        return new LoadableDetachableModel<List<ElementTo>>() {
            @Override
            protected List<ElementTo> load() {
                return elementService.getAllElementTos();
            }
        };
    }

    private void addNewElementLink() {
        Link addElementLink = new Link("createElementLink") {
            @Override
            public void onClick() {
                setResponsePage(new CreateElementPage(null));
            }
        };
        Authorizer.authorize(addElementLink);
        add(addElementLink);
    }

    public void setElementService(ElementServiceImpl elementService) {
        this.elementService = elementService;
    }
}
